package com.epro.dx.activity.order.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.epro.dx.R;
import com.epro.dx.activity.DetailActivity;
import com.epro.dx.activity.order.ShippingInformationActivity;
import com.epro.dx.fragment.BaseLoadAndEmptyDataFragment;
import com.epro.dx.util.ImageLoaderUtils;
import com.epro.dx.widget.MyListView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.weipu.common.constants.ServerConstant;
import com.weipu.common.facade.model.Lines;
import com.weipu.common.facade.model.Package;
import com.weipu.common.facade.model.PackageTrace;

import java.util.ArrayList;

/**
 * @author epro
 * 物流信息多运单号显示页
 */
@SuppressLint("ValidFragment")
public class ShippingTrackingFragment extends BaseLoadAndEmptyDataFragment {

	protected static final String TAG = "ShippingTrackingFragment";
	View rootView;
	private TextView shippingInfoText;
	private TextView shippingInfoTime;
	private MyListView skuListView;
	private RelativeLayout shippingInfo;
	private ImageView point;
	private ImageView arrow;
	
	private DisplayImageOptions options;
	
	private ProductListAdapter productAdapter;
	private Package pack;
	
	private ArrayList<Lines> productList = new ArrayList<Lines>();
	private String currencyCode;
	public ShippingTrackingFragment(Package pack2,String currencyCode){
		this.pack = pack2;
		this.currencyCode = currencyCode;
	}
	public ShippingTrackingFragment(){}
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		if(rootView==null){
			rootView = inflate(R.layout.fragment_shippingtracking);
			options=ImageLoaderUtils.getImageLoaderOptions(R.drawable.item_default,R.drawable.item_default);
			initView();
			initData(savedInstanceState);
			initEvent();
		}
		ViewGroup parent = (ViewGroup) rootView.getParent();
		if(parent!=null){
			parent.removeView(rootView);
		}
		
		return rootView;
	}
	@Override
	public void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		outState.putSerializable("Package", pack);
		outState.putString("CurrencyCode", currencyCode);
//		super.onSaveInstanceState(outState);
	}
	private void initView() {
		shippingInfoText = (TextView) rootView.findViewById(R.id.fragment_shippingtracking_message);
		shippingInfoTime = (TextView) rootView.findViewById(R.id.fragment_shippingtracking_time);
		skuListView = (MyListView) rootView.findViewById(R.id.fragment_shippingtracking_skulist);
		shippingInfo = (RelativeLayout) rootView.findViewById(R.id.fragment_shippingtracking_shippinginfo);
		point = (ImageView) rootView.findViewById(R.id.fragment_shippingtracking_point);
		arrow = (ImageView) rootView.findViewById(R.id.arrow);
	}
	private void initEvent() {
		skuListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// 点击商品列表时进入商品详情页面
				Intent intent = new Intent(getActivity(),
						DetailActivity.class);
				Bundle data = new Bundle();
				data.putInt(ServerConstant.Parameter.PARAMETER_NAME_PRODUCT_ID,
						productList.get(position).getSku());

				data.putString(
						ServerConstant.Parameter.PARAMETER_NAME_BACK_ACTION,
						ServerConstant.ActionConstant.ACTION_SHIPPINGINFO);
				data.putString(
						ServerConstant.Parameter.PARAMETER_NAME_FROM_ACTIVITY,
						ServerConstant.ActionConstant.ACTION_SHIPPINGINFO);

				intent.putExtras(data);
				startActivity(intent);
			}
		});
		
		shippingInfo.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// 进入单个物流信息详情页
				if(pack!=null && pack.getPackageTraces()!=null){
					Intent i = new Intent(getActivity(),ShippingInformationActivity.class);
					Bundle bundle = new Bundle();
					bundle.putParcelableArrayList("PackageTrace", pack.getPackageTraces());
					bundle.putString("TrackingNumber", pack.getTrackingNumber());
					i.putExtras(bundle);
					startActivity(i);
				}
			}
		});
	}
	private void initData(Bundle savedInstanceState) {
		if(savedInstanceState!=null){
			this.pack = (Package) savedInstanceState.getSerializable("Package");
			this.currencyCode = savedInstanceState.getString("CurrencyCode");
		}
		setShippingInfo();
		productList.clear();
		if(pack!=null){
			ArrayList<Lines> products = pack.getProducts();
			if(products!=null && products.size()>0){
				productList.addAll(products);
			}
		}
		productAdapter = new ProductListAdapter();
		skuListView.setAdapter(productAdapter);
	}
	
	/**
	 * 设置显示物流信息
	 */
	public void setShippingInfo(){
		if(pack!=null){
			ArrayList<PackageTrace> packageTraces = pack.getPackageTraces();
			if(packageTraces!=null){
				shippingInfoText.setTextColor(android.graphics.Color.parseColor("#FF6600"));
				shippingInfoText.setText(packageTraces.get(0).getTrace());
				shippingInfoTime.setText(packageTraces.get(0).getTraceTime());
				point.setVisibility(View.VISIBLE);
				arrow.setVisibility(View.VISIBLE);
			}else{
				shippingInfoText.setTextColor(android.graphics.Color.parseColor("#999999"));
				shippingInfoText.setText(pack.isTracking()?R.string.shippingTracking_no_message:R.string.shippingTracking_no_tracking);
//				shippingInfoText.setText(TextUtils.isEmpty(pack.getTrackingNumber())?R.string.shippingTracking_no_tracking:R.string.shippingTracking_no_message);
				shippingInfoTime.setVisibility(View.GONE);
				point.setVisibility(View.GONE);
				arrow.setVisibility(View.GONE);
			}
		}else{
			shippingInfoText.setTextColor(android.graphics.Color.parseColor("#999999"));
			shippingInfoText.setText(R.string.shippingTracking_no_tracking);
			shippingInfoTime.setVisibility(View.GONE);
			point.setVisibility(View.GONE);
			arrow.setVisibility(View.GONE);
		}
	}
	
	class ProductListAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return productList.size();
		}

		@Override
		public Lines getItem(int position) {
			return productList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if(convertView==null){
				holder = new ViewHolder();
				convertView = View.inflate(getActivity(), R.layout.item_shippinginfo_productlist, null);
				holder.price = (TextView) convertView.findViewById(R.id.tv_prodcut_price);
				holder.productImg = (ImageView) convertView.findViewById(R.id.iv_img_product);
				holder.productName = (TextView) convertView.findViewById(R.id.tv_productname);
				holder.qty = (TextView) convertView.findViewById(R.id.tv_shipping);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			Lines product = productList.get(position);
			String smileImageUrl = product.getSmileImageUrl();
			if(smileImageUrl!=null && !TextUtils.isEmpty(smileImageUrl)){
				ImageLoader.getInstance().displayImage(smileImageUrl, holder.productImg, options);
			}
			
			holder.price.setText(currencyCode+product.getUnitPrice());
			holder.productName.setText(product.getName());
			holder.qty.setText(product.getOrderedQty()+"");
			return convertView;
		}
		
	}
	class ViewHolder{
		TextView productName;
		TextView price;
		TextView qty;
		ImageView productImg;
	}
}
